package cn.uyunsky.platform.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * @version 1.0
 * @author baiyh
 * @date 2012-3-4
 */
public class TextAreaInputTag extends BodyTagSupport {

	private static final long serialVersionUID = 1L;

	public TextAreaInputTag() {
	}

	private static final String baseStyle = " uyunsky_base uyunsky_textarea";

	private String id = null;
	private String styleClass = null;
	private String style = null;
	private boolean require = false;

	public void setId(String id) {
		this.id = id;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public void setRequire(boolean require) {
		this.require = require;
	}


	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}



	public void release() {
		super.release();
		id = null;
		styleClass = null;
		style = null;
	}

	public int doEndTag() throws JspException {
		return super.doEndTag();
	}

	public int doStartTag() throws JspException {
		write(this.pageContext, this.renderInputElement());
		return super.doStartTag();
	}

	protected String renderInputElement() throws JspException {
		StringBuffer results = new StringBuffer("<textarea");

		prepareAttribute(results, "id", id);

		prepareAttribute(results, "style", style);
		if (!isEmpty(getStyleClass())) {
			prepareAttribute(results, "class", getStyleClass());
		}

		results.append(this.getElementClose());

		if (require) {
			prepareRequired(results);
		}
		return results.toString();
	}

	public String getStyleClass() {
		StringBuffer clazz = new StringBuffer(baseStyle);

		if (styleClass != null) {
			clazz.append(" ").append(styleClass);
		}

		if (require) {
			clazz.append(" ").append("require");
		}
		return clazz.toString();
	}

	private void prepareRequired(StringBuffer results) {
		results.append("<font color=\"red\">*</font>");
	}

	private String getElementClose() {
		return "></textarea>";
	}

	/**
	 * @param handlers
	 * @param name
	 * @param value
	 * void
	 */
	protected void prepareAttribute(StringBuffer handlers, String name, Object value) {
		if (value != null) {
			handlers.append(" ");
			handlers.append(name);
			handlers.append("=\"");
			handlers.append(value);
			handlers.append("\"");
		}
	}

	/**
	 * @param handlers
	 * @param name
	 * @param value
	 * void
	 */
	protected void prepareAttribute2(StringBuffer handlers, String name, Object value) {
		if (value != null) {
			handlers.append(" ");
			handlers.append(name);
			handlers.append("='");
			handlers.append(value);
			handlers.append("'");
		}
	}

	private void write(PageContext pageContext, String text) throws JspException {
		JspWriter writer = pageContext.getOut();
		try {
			writer.print(text);
		} catch (IOException e) {
			throw new JspException("uynsky tld error", e);
		}
	}

	protected boolean isEmpty(String val) {
		if (val == null || val.length() == 0)
			return true;
		else
			return false;
	}
}
